/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.TreeView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;

public class LocalFileTransferTask extends Task<HashMap<String,String>>{
    private File source=null;
    private String destPath;
    private HashMap<String,String> successInfo;
    private UploadListItem item=null;
    
    public int STOP=-2;
    public int SUSPEND=-1;
    public int RUNNING=1;
    public int status=0;
    
    
    public LocalFileTransferTask(File source,String destPath,UploadListItem item){
        this.source=source;
        this.destPath=destPath;
        this.item=item;
    }
    
    @Override
    protected HashMap<String, String> call() throws Exception{
        successInfo=new HashMap<String,String>();
        
//        FileInputStream in=new FileInputStream(source);
//        destPath=destPath+"/"+item.getBox().getUserData().toString();
//        System.out.println("destPath:"+destPath);
//        File target=new File(destPath);
//        FileOutputStream out=new FileOutputStream(target);
//                
//        byte[] buffer=new byte[1024*10];
//        long size=source.length();
//        long starttime=new Date().getTime();
//
//        int ins=0;
//        int rlength=0;
//        while((ins= in.read(buffer, 0, buffer.length)) != -1){            
//            out.write(buffer,0,ins);
//            rlength+=ins;
//            updateProgress(rlength,size);
//            Thread.sleep(10);
//        }
//        in.close();
//        out.flush();
//        out.close();
//        
//        long endtime=new Date().getTime()-starttime;
        write();
        
        return successInfo;
    }
    
    public File getFile() {
        return source;
    }

    public ReadOnlyDoubleProperty getProgressProperty() {
        return this.progressProperty();
    }

    public void setOnFinishHander(EventType<WorkerStateEvent> type, EventHandler<WorkerStateEvent> handler) {
        this.addEventHandler(type,handler);
    }

    public HashMap<String, String> getSuccessInfo() {
        return successInfo;
    }
    
    public synchronized void write() throws FileNotFoundException, IOException, InterruptedException{
        successInfo=new HashMap<String,String>();
        
        FileInputStream in=new FileInputStream(source);
        destPath=destPath+"/"+item.getBox().getUserData().toString();
        //System.out.println("destPath:"+destPath);
        File target=new File(destPath);
        FileOutputStream out=new FileOutputStream(target);
                
        byte[] buffer=new byte[1024*10];
        long size=source.length();
        long starttime=new Date().getTime();

        int ins=0;
        int rlength=0;
        while((ins= in.read(buffer, 0, buffer.length)) != -1){
            if(status != STOP){
                if(status == SUSPEND){
                    try{
                       wait();
                    }catch (InterruptedException e){
                       e.printStackTrace();
                    }
                }else{
                    System.out.println(Thread.currentThread().getState());
                    out.write(buffer,0,ins);
                    rlength+=ins;
                    updateProgress(rlength,size);
                    Thread.sleep(10);
                }
            }else{
                in.close();
                out.flush();
                out.close();
                target.delete();
                System.out.print("stop");
                break;  
            }
        }
        in.close();
        out.flush();
        out.close();
        
        long endtime=new Date().getTime()-starttime;
    }

    public synchronized void resume(){
      status = RUNNING;
      notifyAll();
    }

    public void suspend(){
      status = SUSPEND;
    }

    public void stop(){
      status = STOP;
    }
}
